package cn.fch.gotrip.api;

import lombok.Data;
import org.springframework.lang.Nullable;

/**
 * @program: gotrip
 * @description:
 * @CreatedBy: fch
 * @create: 2023-01-14 17:13
 **/
@Data
public class BeanResponse {
    private Boolean success;
    private Integer code;
    private String message;
    private Object data;

    // 构造方法设为私有
    private BeanResponse() {
    }

    public static BeanResponse ok(Object params) {
        BeanResponse beanResponse = new BeanResponse();
        beanResponse.setSuccess(ResultEnum.OK.getSuccess());
        beanResponse.setCode(ResultEnum.OK.getCode());
        beanResponse.setMessage(ResultEnum.OK.getMessage());
        beanResponse.setData(params);
        return beanResponse;
    }

    public static BeanResponse ok(Object params, String message) {
        BeanResponse beanResponse = new BeanResponse();
        beanResponse.setSuccess(ResultEnum.OK.getSuccess());
        beanResponse.setCode(ResultEnum.OK.getCode());
        beanResponse.setMessage(message);
        beanResponse.setData(params);
        return beanResponse;
    }

    public static BeanResponse badRequest(@Nullable String message) {
        BeanResponse beanResponse = new BeanResponse();
        beanResponse.setSuccess(ResultEnum.BAD_REQUEST.getSuccess());
        beanResponse.setCode(ResultEnum.BAD_REQUEST.getCode());
        beanResponse.setMessage(message != null ? message : ResultEnum.BAD_REQUEST.getMessage());
        beanResponse.setData(null); // 校验失败不返回参数
        return beanResponse;
    }

    public static BeanResponse unauthorized(@Nullable String message) {
        BeanResponse beanResponse = new BeanResponse();
        beanResponse.setSuccess(ResultEnum.UNAUTHORIZED.getSuccess());
        beanResponse.setCode(ResultEnum.UNAUTHORIZED.getCode());
        beanResponse.setMessage(message != null ? message : ResultEnum.UNAUTHORIZED.getMessage());
        beanResponse.setData(null); // 校验失败不返回参数
        return beanResponse;
    }

    public static BeanResponse notFound(@Nullable String message) {
        BeanResponse beanResponse = new BeanResponse();
        beanResponse.setSuccess(ResultEnum.NOT_FOUND.getSuccess());
        beanResponse.setCode(ResultEnum.NOT_FOUND.getCode());
        beanResponse.setMessage(message != null ? message : ResultEnum.NOT_FOUND.getMessage());
        beanResponse.setData(null); // 校验失败不返回参数
        return beanResponse;
    }
}